package offer;

/**
 * @Author Elephas
 * @Date 2022/1/28
 **/
@FunctionalInterface
public interface AddBinary {
    String addBinary(String a, String b);
}
class AddBinaryImpl1 implements AddBinary{
    /*
        将两个字符串逆置，记录进位
     */
    @Override
    public String addBinary(String a, String b) {
        boolean C = false;
        int strLen = a.length() > b.length() ? a.length():b.length();
        /*
            初始化辅助数组
         */
        char[] strA = new char[strLen];
        char[] strB = new char[strLen];
        StringBuilder ans = new StringBuilder();
        a = new StringBuilder(a).reverse().toString();
        for (int i = 0; i < a.length(); i++) {
            strA[i] = a.charAt(i);
        }
        for (int i = a.length(); i < strLen; i++) {
            strA[i] = '0';
        }
        b = new StringBuilder(b).reverse().toString();
        for (int i = 0; i < b.length(); i++) {
            strB[i] = b.charAt(i);
        }
        for (int i = b.length(); i < strLen; i++) {
            strB[i] = '0';
        }

        // 处理进位
        for (int i = 0; i < strLen; i++) {
            if(C){
                if(strA[i] == '0' && strB[i] == '0'){
                    C = false;
                    ans.append('1');
                }else if(strA[i] == '1' && strB[i] == '1'){
                    C = true;
                    ans.append('1');
                }else{
                    C = true;
                    ans.append('0');
                }
            }else {
                if(strA[i] == '1' && strB[i] =='1'){
                    C = true;
                    ans.append('0');
                }else if(strA[i] == '0' && strB[i] == '0'){
                    C = false;
                    ans.append('0');
                }else{
                    C = false;
                    ans.append('1');
                }
            }
        }
        if(C){
            ans.append('1');
        }
        return ans.reverse().toString();
    }
}

class AddBinaryImpl2 implements AddBinary{

    @Override
    public String addBinary(String a, String b) {
        int strLen = a.length() > b.length()? a.length():b.length();
        boolean carry = false;
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < strLen; i++) {
            char digitA = i < a.length() ? a.charAt(a.length() - i - 1) : '0';
            char digitB = i < b.length() ? b.charAt(b.length() - i - 1) : '0';
            if(carry){
                if(digitA == '0' && digitB == '0'){
                    carry = false;
                    ans.append('1');
                }else if(digitA == '1' && digitB == '1'){
                    carry = true;
                    ans.append('1');
                }else{
                    carry = true;
                    ans.append('0');
                }
            }else{
                if(digitA == '0' && digitB == '0'){
                    carry = false;
                    ans.append('0');
                }else if(digitA == '1' && digitB == '1'){
                    carry = true;
                    ans.append('0');
                }else{
                    carry = false;
                    ans.append('1');
                }
            }
        }
        if(carry){
            ans.append('1');
        }
        return ans.reverse().toString();
    }
}